#include "RunningSoldierState.h"




void RunningSoldierState::_initDirection()
{
	double targetx = _armyData->targetRect->X;
	double x = _armyData->rect->X;

	int direction;

	if (x < targetx) {
		direction = _RUNNING_SOLDIER_DIRECTION_RIGHT;
	} else {
		direction = _RUNNING_SOLDIER_DIRECTION_LEFT;
	}

	_armyData->sprites->currentSprite->setStartEnd(direction, direction + 3);
	_armyData->sprites->currentSprite->setIndex(direction);
	_armyData->sprites->currentSprite->setDelay(8);
}

void RunningSoldierState::_checkTargetInRange()
{
	double targetx = _armyData->targetRect->X;
	double x = _armyData->rect->X;
	double range = _armyData->rangeOfEffect->Width;

	if (abs(x - targetx) < range) {
		_armyData->isActived = true;
	}
}

void RunningSoldierState::_updateSprites()
{
	if (_armyData->isActived) {
		_armyData->sprites->updateCurrent();
	} else {
		_checkTargetInRange();
	}
}

RunningSoldierState::RunningSoldierState(ArmyData* armyData) : RedSoldierState(armyData)
{
	_armyData->isActived = false;
}

void RunningSoldierState::draw(long timetick)
{
	if (_armyData->isActived)
		_armyData->sprites->draw();
}

void RunningSoldierState::initSprites()
{
	_armyData->initSpriteHandler();
	_armyData->sprites->addSprite(1, _ID_ARMY_RUNNING_SOLDIER, 0);
	_armyData->sprites->setState(1);

	_initDirection();
}
